package com.om.Module.openeuler;

import com.om.common.CommonObject;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.function.Function;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;


/**
 * 功能描述:解析清华镜像站中openeuler数据
 * @Author: xiazhonghai
 * @Date: 2020/12/1 14:35
 */
public class morrirs_Openeuler_Recoeds extends CommonObject {
    @Override
    public HashMap assemblydata(Object stementstr, String id) throws IOException, KeyManagementException, NoSuchAlgorithmException {
        String stement=(String)stementstr;
        String[] s1 = stement.split(" ", 4);
        HashMap hashMap = new HashMap<>();
        hashMap.put("ip", s1[0]);
        hashMap.put("remote_user", "");
        String[] split = s1[3].split("]", 2);
        hashMap.put("created_at", formatDate(split[0]));
        String[] split1 = split[1].trim().split("\"", 3);
        hashMap.put("path", split1[1].split(" ")[1]);
        String[] s2 = split1[2].trim().split(" ", 3);
        hashMap.put("status", s2[0]);
        hashMap.put("body_bytes_sent", s2[1]);
        String[] split2 = s2[2].trim().split("\"", 3);
        hashMap.put("sent_http_content_type", split2[1]);
        String[] split3 = split2[2].trim().split("\"", 3);
        hashMap.put("http_referer", split3[1]);
        String[] split4 = split3[2].trim().split("\"", 3);
        hashMap.put("http_user_agent", split4[1]);
        String[] s3 = split4[2].trim().split(" ");
        hashMap.put("scheme", s3[1]);
        if(hashMap.get("ip").toString().startsWith("100")){
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("lon",null);
            objectObjectHashMap.put("lat",null);
            hashMap.put("location",objectObjectHashMap);
        }else {
            HashMap geoipMap = getLocationByIP(hashMap.get("ip").toString(), this.getEsIndexName(), id);
            hashMap.putAll( geoipMap);
        }
        return hashMap;
    }
    public static String formatDate(String datestr) {
        //datestr [08/Oct/2020:00:03:23 +0800
        String[] splitdate = datestr.substring(1).split("/", 3);
        //08 Oct 2020:00:03:23 +0800
        String day = splitdate[0];
        String month = splitdate[1];
        switch (month) {
            case "Jan":
                month = "01";
                break;
            case "Feb":
                month = "02";
                break;
            case "Mar":
                month = "03";
                break;
            case "Apr":
                month = "04";
                break;
            case "May":
                month = "05";
                break;
            case "Jun":
                month = "06";
                break;
            case "Jul":
                month = "07";
                break;
            case "Aug":
                month = "08";
                break;
            case "Sep":
                month = "09";
                break;
            case "Oct":
                month = "10";
                break;
            case "Nov":
                month = "11";
                break;
            case "Dec":
                month = "12";
                break;
            default:
        }
        String[] splittime = splitdate[2].split(":", 4);
        //2020 00 03 23 +0800
        String year = splittime[0];
        String hour = splittime[1];
        String minute = splittime[2];
        String[] timezone = splittime[3].split(" ", 2);
        String second = timezone[0];
        String result = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
        return result;
    }
    /**
     * 功能描述:过滤数据
     * @param srcRdd: 原始rdd
     * @param vhosts: 指定需要获取的vhost
     * @return: org.apache.spark.api.java.JavaRDD
     * @Author: xiazhonghai
     * @Date: 2020/12/1 14:36
     */
    @Override
    public JavaRDD dealData(JavaRDD srcRdd,String[] vhosts){
        JavaRDD<String> filterDatas = srcRdd.filter((Function) v1 -> ((String)v1).contains(vhosts[0]));
        return filterDatas;
    }

}
